#include <malloc.h>
#include <stdio.h>

char *replaceSpaces(char *S, int length) {
    //char *p = (char *)malloc(sizeof(char) * (3 * length + 1));
    char *p;
    p = malloc(sizeof(char) * (3 * length + 1));
    int i, j;
    for (i = 0, j = 0; i < length; i++) {
        if (S[i] == ' ') {
            p[j++] = '%';
            p[j++] = '2';
            p[j++] = '0';
        } else {
            p[j++] = S[i];
        }
    }
    p[j] = '\0';
    return p;
}

int main() {
    char *ch = "Mr John Smith    ";
    int len = 17;
    char *replace = replaceSpaces(ch, len);
    printf("%s", replace);
}